package me.zhengjie.modules.management.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.management.domain.ConferenceRecord;
import me.zhengjie.modules.management.domain.MrConferencePurpose;
import me.zhengjie.modules.management.mapper.*;
import me.zhengjie.modules.management.service.EmailDtoService;
import me.zhengjie.modules.management.service.dto.EmailDto;
import me.zhengjie.modules.management.service.dto.MrRecordDtlDto;
import me.zhengjie.modules.management.service.dto.RecordDto;
import me.zhengjie.modules.system.repository.DeptRepository;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: chenxin.jiang
 * @Date: 2021/1/1
 * @Description: 构造邮件信息service
 */
@Service
@RequiredArgsConstructor
public class EmailDtoServiceImpl implements EmailDtoService {

    private final ConferenceRecordMapper conferenceRecordMapper;

    private final UserMapper userMapper;

    private final MrRecordDtlMapper mrRecordDtlMapper;

    private final DeptRepository deptRepository;

    private final MrConferencePurposeMapper mrConferencePurposeMapper;

    public EmailDto getEmailDto(Integer recordId){
        EmailDto emailDto = new EmailDto();
        //封装邮件信息
        List<RecordDto> recordDtoList = conferenceRecordMapper.findById(recordId);
        //获取1.培训类型；2.教室费用；3.物资费用
        ConferenceRecord cr = conferenceRecordMapper.queryById(recordId);

        RecordDto recordDto = recordDtoList.get(0);
        emailDto.setSubject(recordDto.getSubject()); //培训名称
        emailDto.setRoomName(recordDto.getRoomName()); //教室地点
        emailDto.setTypeName(mrConferencePurposeMapper.getMrConferencePurposeById(cr.getTypeId()).getPurposeName());//培训类型
        emailDto.setUserNumber(recordDto.getUserNumber()); //参训人数
        //基础费用
        emailDto.setRoomCost(cr.getRoomCost());
        //物资费用
        emailDto.setMaterialCost(cr.getMaterialsCost());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        emailDto.setBeginDate(sdf.format(new Date(recordDto.getBeginDate().getTime())));  //开始时间
        emailDto.setEndDate(sdf.format(new Date(recordDto.getEndDate().getTime())));
       /* emailDto.setBeginDate(recordDto.getBeginDate().toString());  //开始时间
        emailDto.setEndDate(recordDto.getEndDate().toString());   //结束时间*/
        //申请人
        emailDto.setApplicant(userMapper.findNameById(recordDto.getApplicantId().longValue()));
        //申请人部门
        emailDto.setApplicantDept(deptRepository.findNameById(userMapper.findDeptIdById(recordDto.getApplicantId().longValue())));
        //封装选择物资信息
        List<MrRecordDtlDto> mrRecordDtls = mrRecordDtlMapper.getListById(recordDto.getRecordId());
        if (mrRecordDtls.size()>0){
            emailDto.setMaterialDtl(mrRecordDtls);
        }else{
            MrRecordDtlDto mrRecordDtlDto = new MrRecordDtlDto();
            mrRecordDtlDto.setMaterialName("无");
            mrRecordDtlDto.setNumber(new BigDecimal(Double.valueOf(0)));
            mrRecordDtlDto.setPrice(new BigDecimal(Double.valueOf(0)));
            mrRecordDtlDto.setAmts(new BigDecimal(Double.valueOf(0)));
            mrRecordDtls.add(mrRecordDtlDto);
            emailDto.setMaterialDtl(mrRecordDtls);
        }

        return emailDto;
    }
}
